/*kZime*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#define MAXN
using namespace std;
inline int read() {
	int k = 0, f = 1; char c = getchar();
	for(; !isdigit(c); c = getchar())if(c == '-') f = -1;
	for(; isdigit(c); c = getchar()) k = k * 10 + c - '0';
	return k * f;
}
/*-----------------------------------------------------------------------------*/
int q[3000];
int n, head, tail; 
char ans[3000];
int AC() {
#ifndef MYLAB
	freopen("queue.in", "r", stdin);
	freopen("queue.out", "w", stdout);
#else
	freopen("in.txt", "r", stdin);
#endif
	n = read();
	while(n--) {
		int t = read();
		if(t == 1)tail = head;
		else if(t == 2) {
			q[head++] = read();
			if(head - tail > 10) {
				printf("queue out\n");
				head--;
			}
		}else if(t == 3) {
			tail++;
			if(tail > head) {
				printf("queue empty\n");
				tail--;
			}
		}else if(t == 4) {
			printf("%d\n", head - tail);
			for(int i = tail; i < head; i++) {
				printf("%d ", q[i]);
			}
			printf("\n");
		}
	}
	return 0;
}
int HA = AC();
int main(){;}

